package tech.dort.dortware.impl.modules.exploit;

import com.google.common.eventbus.Subscribe;
import net.minecraft.network.play.client.C00PacketKeepAlive;
import net.minecraft.network.play.client.C0FPacketConfirmTransaction;
import tech.dort.dortware.api.module.Module;
import tech.dort.dortware.api.module.ModuleData;
import tech.dort.dortware.api.property.impl.BooleanValue;
import tech.dort.dortware.api.property.impl.NumberValue;
import tech.dort.dortware.impl.events.PacketEvent;
import tech.dort.dortware.impl.utils.networking.PacketSleepThread;

public class PingSpoof extends Module {

    private final NumberValue delay = new NumberValue("Delay", this, 100, 50, 10000, true);
    public final BooleanValue transaction = new BooleanValue("Transactions", this, true);
    public final BooleanValue keepAlives = new BooleanValue("Keep Alives", this, true);
    public final BooleanValue cancel = new BooleanValue("Cancel", this, true);

    public PingSpoof(ModuleData moduleData) {
        super(moduleData);
        register(delay, transaction, keepAlives, cancel);
    }

    @Subscribe
    public void onPacket(PacketEvent event) {
        if (cancel.getValue()) {
            event.forceCancel(keepAlives.getValue() && event.getPacket() instanceof C00PacketKeepAlive || transaction.getValue() && event.getPacket() instanceof C0FPacketConfirmTransaction);
        } else {
            if (event.getPacket() instanceof C0FPacketConfirmTransaction && transaction.getValue()) {
                PacketSleepThread.delayPacket(event.getPacket(), delay.getValue().longValue());
                event.setCancelled(true);
            }
            if (event.getPacket() instanceof C00PacketKeepAlive && keepAlives.getValue()) {
                PacketSleepThread.delayPacket(event.getPacket(), delay.getValue().longValue());
                event.setCancelled(true);
            }
        }
    }

}
